//
//  FreightStatisticsVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/21.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "FreightStatisticsVC.h"
#import "FreightStatisticsCell.h"
#import "FreightStatistModel.h"
#import "SourcesOrderListDetailVC.h"
@interface FreightStatisticsVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView *header;
    FlexFrameView *bottomView;
}
@property (nonatomic,strong) FreightStatisticsCell *freightStatisticsCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) CJLabel *labStartTime;
@property (nonatomic,strong) CJLabel *labEndTime;
//@property (nonatomic,strong) CJLabel *labSelectType;
@property (nonatomic,strong) CJTextField *fieldSerach;
@property (nonatomic,copy) NSString *strStartTime;
@property (nonatomic,copy) NSString *strEndTime;
//@property (nonatomic,copy) NSString *strSelectType;
@property (nonatomic,strong) CJLabel *labShipperDeliveryFee; //总费用
@property (nonatomic,strong) CJLabel *labTaxPayFee; //服务总费用
@property (nonatomic,strong) CJLabel *labTransportationCost; //运输
@end

@implementation FreightStatisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"运费统计"];
    [self setBackLeftButton:@""];
      
    [self setRightImage:@"重置" image:@""];
    self.freightStatisticsCell = [[FreightStatisticsCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self createTopView];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
    [self setDefalutValue];
}

- (void)setDefalutValue{
    self.strStartTime = @"";
    self.strEndTime = @"";
    //    self.strSelectType = @"";
    self.labStartTime.text = @"开始时间";
    self.labEndTime.text = @"结束时间";
    self.fieldSerach.text = @"";
}

- (void)RightTextClick:(UIButton *)sender{
    [self setDefalutValue];
    [self.tableView.mj_header beginRefreshing];
}

- (void)createTopView{
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"FreightStatisticsHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;

    [header layoutIfNeeded];
    [self.view addSubview:header];
    
    CGRect rcFrame1 = CGRectMake(0, kScreenHeight - 90, [[UIScreen mainScreen] bounds].size.width ,0);
    bottomView = [[FlexFrameView alloc] initWithFlex:@"FreightStatisticsBottomView" Frame:rcFrame1 Owner:self];
    bottomView.flexibleHeight = YES;

    [bottomView layoutIfNeeded];
    [self.view addSubview:bottomView];
}

- (void)btnSearchDataAction{
    [self.tableView.mj_header beginRefreshing];
}

- (void)btnSelectStartTimeAction{
    [self btnShowDataTimeWithType:@"0"];
}
- (void)btnSelectEndimeAction{
    [self btnShowDataTimeWithType:@"1"];
}

- (void)btnShowDataTimeWithType:(NSString *)strType{
    
    WS(ws);
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = strType.intValue == 0 ? @"开始时间":@"截止时间";
    datePickerView.minDate = [NSDate br_setYear:2016 month:5 day:1];
    datePickerView.maxDate = [NSDate date];  //[NSDate br_setYear:2050 month:5 day:1];
    datePickerView.resultBlock = ^(NSString *selectValue) {

        if (strType.intValue == 0) {
            ws.labStartTime.text = selectValue;
            ws.strStartTime = CJStringWithFormat(@"%@ 00:00:00",selectValue);
        } else {
            ws.labEndTime.text = selectValue;
            ws.strEndTime = CJStringWithFormat(@"%@ 00:00:00",selectValue);
        }
        [ws.modelArray removeAllObjects];
        [ws.tableView.mj_header beginRefreshing];
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];

}

/// <#Description#>
- (void)loadData {
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/costStatistics",CJBaseUrl);
    NSDictionary *dic = @{
        @"pageNum":CJStringWithFormat(@"%d",self.page),
        @"pageSize":@"20",
        @"searchInfo":[CJControl getThreeUnaryValueStr:self.fieldSerach.text],
        @"settleTimeStart":self.strStartTime,
        @"settleTimeEnd":self.strEndTime
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:GetType url:strUrl parameters:dic status:ArpaDefault success:^(id  _Nonnull responseObject) {
        
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        NSDictionary *dicSum = responseObject[@"sumObject"];

        if ([ws.refreshType isEqualToString:@"down"]) {
           [ws.modelArray removeAllObjects];
        }

        NSError * error ;
        NSArray *arr = [FreightStatistModel arrayOfModelsFromDictionaries:list error:&error];
        [ws.modelArray addObjectsFromArray:arr];

        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];

        if (ws.modelArray.count == total.intValue) {
           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        NSString *shipperDeliveryFee = [CJControl getThreeUnaryValueStr:dicSum[@"shipperDeliveryFee"]];
        NSString *transportationCost = [CJControl getThreeUnaryValueStr:dicSum[@"transportationCost"]];
        NSString *taxPayFee = [CJControl getThreeUnaryValueStr:dicSum[@"taxPayFee"]];

        ws.labShipperDeliveryFee.text = @([shipperDeliveryFee floatValue]).cj_money_description;
        ws.labTaxPayFee.text = @([taxPayFee floatValue]).cj_money_description;
        ws.labTransportationCost.text = @([transportationCost floatValue]).cj_money_description;

//        ws.labSumMoney.text = CJStringWithFormat(@"总运费总计：%@元\n服务费总计：%@元\n运输费用总计：%@元 ",,@([ floatValue]).cj_money_description ,@([ floatValue]).cj_money_description );
        [ws.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];
    
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"FreightStatisticsCell";
    FreightStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[FreightStatisticsCell alloc] initWithFlex:@"FreightStatisticsCell" reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    FreightStatistModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FreightStatistModel *model = self.modelArray[indexPath.row];
    SourcesOrderListDetailVC *vc = [[SourcesOrderListDetailVC alloc]init];
    vc.strCode = model.code;
    [self pushVc:vc];
    
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + header.height + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - header.height - CJTableToTopHeight  - 90),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}

@end
